/**
 * Created by Administrator on 2017/3/2 0002.
 */
var express=require("express");
var mongoose=require('mongoose')
var bodyParser=require('body-parser')
var path=require('path')
var _=require('underscore')
var Movie=require('./models/movie')
var port=process.env.PORT ||3000
var fs = require("fs");
var data = '菜鸟教程官网地址：www.runoob.com';

// 创建一个可以写入的流，写入到文件 output.txt 中
var writerStream = fs.createWriteStream('output.txt');

// 使用 utf8 编码写入数据
writerStream.write(data,'UTF8');

// 标记文件末尾
writerStream.end();

// 处理流事件 --> data, end, and error
writerStream.on('finish', function() {
    console.log("写入完成。");
});

writerStream.on('error', function(err){
    console.log(err.stack);
});

console.log("程序执行完毕");
var app=express()

mongoose.connect('mongodb://localhost:3000/imooc')

app.set('views','./views/pages')
app.set('view engine','pug')
app.use(express.static(path.join(__dirname,'content')))
app.use(bodyParser.urlencoded({extended:false}))
app.listen(port)
console.log(" imooc started on port"+port)
//index page
app.get("/",function(req,res){
    Movie.fetch(function(err,movies){
        if(err){
            console.log(err)
        }
        res.render("index",{
            title:'imooc 首页',
            movies:movies
        })
    })

})
//detail page
app.get("/movie/:id",function(req,res){
    var id=req.params.id
    Movie.findById(id,function(err,movie){
        res.render("detail",{
            title:'imooc'+movie.title,
            movie:movie
        })
    })

})
//admin updata movie
app.get('/admin/updata/:id',function(err,res){
    var id=req.params.id
    if(id){
        Movie.findById(id,function(err,movie){
            res.render('admin',{
                title:'imooc 后台更新页',
                movie:movie
            })
        })
    }
})
//admin page
app.get("/admin/movie",function(req,res){
    res.render("admin",{
        title:'imooc 后台录入页',
        movie:{
            title:'',
            doctor:'',
            country:'',
            year:'',
            poster:'',
            flash:'',
            summary:'',
            language:''
        }
    })
})
//admin post movie
app.post("/admin/movie/new",function(req,res){
    var id=req.body.movie._id
    var movieObj=req.body.movie
    var _movie
    if(id!=='undefined'){
        Movie.findById(id,function(err,movie){
            if(err){
                console.log(err)
            }
            _move= _.extend(movie,movieObj)
            _movie.save(function(err,movie){
                if(err){
                    console.log(err)
                }
                res.redirect('/movie/'+movie._id)
            })
        })
    }
    else{
        _movie=new Movie({
            doctor:movieObj.doctor,
            title:movieObj.title,
            country:movieObj.country,
            language:movieObj.language,
            year:movieObj.year,
            poster:movieObj.poster,
            summary:movieObj.summary,
            flash:movieObj.flash
        })
        _movie.save(function(err,movie){
            if(err){
                console.log(err)
            }
            res.redirect('/movie/'+movie._id)
        })
    }
})
//list page
app.get("/admin/list",function(req,res){
    Movie.fetch(function(err,movies){
        if(err){
            console.log(err)
        }
        res.render("list",{
            title:'imooc 列表页',
            movies:movies
        })
    })

})
